package cn.itcast.web.controller.cargo;


import cn.itcast.domain.ResultInfo;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhou
 * @since 2021-01-28
 */
@Controller
@RequestMapping("/cargo/finance")
public class FinanceController extends BaseController {

    @Reference
    private FinanceService financeService;
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;

    @RequestMapping("/save.do")
    public String save(Finance finance){
        finance.setCompanyId(getLoginUserCompanyId());
        financeService.save(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping("list.do")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        //获取companyId
        String loginUserCompanyId = getLoginUserCompanyId();
        //根据companyId查询该公司所有财务报表信息
        PageInfo<Finance> pageInfo = financeService.findByPage(pageNum,pageSize,loginUserCompanyId);

        //保存到请求域
        request.setAttribute("pageInfo",pageInfo);

        //返回到list页面
        return "cargo/finance/finance-list";
    }

    @ResponseBody
    @RequestMapping("/exportPdf")
    public void exportPdf(String id) throws Exception {
        //通知浏览器以附件下载的形式处理
        response.setHeader("content-disposition","attachment;filename=finance.pdf");

        //1. 获取到模板的输入流
        InputStream inputStream = session.getServletContext().getResourceAsStream("/jasper/finance.jasper");

        List<Object> list = new ArrayList<>();
        //2. 根据id查找报运单
        Finance finance = financeService.findById(id);
        String exportNos = finance.getExportNos();
        String[] exportIds = exportNos.split(" ");
        for (String exportId : exportIds) {
            Export export = exportService.findById(exportId);
            ExportProductExample exportProductExample = new ExportProductExample();
            exportProductExample.createCriteria().andExportIdEqualTo(exportId);
            List<ExportProduct> exportProducts = exportProductService.findAll(exportProductExample);
            for (ExportProduct exportProduct : exportProducts) {
                list.add(exportProduct);
            }
        }

        //3. 把报运单的对象转换为map
        Map<String, Object> map = BeanMapUtils.beanToMap(finance);

        // 创建javabena数据源对象
//        List<Map<String,Object>> list = new ArrayList<>();
        list.add(map);

        //5. 把报运单的商品存储到数据源中
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, map,dataSource);

        //6. 把pdf输出即可
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

//    /cargo/finance/toView.do?id="+id
//    @RequestMapping("/toView.do")
//    public String toView(@RequestParam("id") String financeListId){
//        PackingList financeList = financeOrderService.getById(financeListId);
//
//        request.setAttribute("finance",financeList);
//
//        return "cargo/finance/finance-view";
//    }

//    @RequestMapping("/delete.do")
//    public String delete(@RequestParam("id") String financeId){
//        ResultInfo resultInfo = financeService.delete(financeId);
//
//        if (resultInfo.getSuccess()){
//            return "redirect:/cargo/finance/list.do";
//        }else {
//            request.setAttribute("error",resultInfo.getMsg());
//            return "forward:/error-info.jsp";
//        }
//
//    }
//
//    @RequestMapping("/finance.do")
//    public String Finance(@RequestParam("id") String financeId){
//        ResultInfo resultInfo = financeService.finance(financeId);
//
//        if (resultInfo.getSuccess()){
//            //若结果为true，data存储到请求域中
//            request.setAttribute("finance", resultInfo.getData());
//
//            //跳转到添加委托信息页
//            return "cargo/finance/finance-finance";
//        }else {
//            //若结果为fasle，跳转到失败页
//            request.setAttribute("error",resultInfo.getMsg());
//            return "forward:/error-info.jsp";
//        }
//    }
}


